<?php

/**
 * SPU品牌聚合
 *
 * @author    Y.N
 * @update	2011/5/13
 */
class CLI_Aggregate2brand extends CLI
{

	/**
	 * CLI主方法
	 *
	 * @param	none
	 * @access	public
	 * @return	void
	 * @update	2011/5/13
	*/
	public function main()
	{
		$date = date('Ymd');

		$filename = TMPPATH.'/logs/cli/'.$date.'.sql';
		if ( ! file_exists($filename))
		{
			$content = "set names Utf8;\r\n";
			$content .= "use hqb;\r\n";
			@file_put_contents($filename, $content);
		}
		self::brand_catch_goods();

		CLI::output("\n品牌聚合完毕\n");
		exit;

	} // end func

	/**
	 * 品牌抓取商品
	 *
	 * @param	none
	 * @access	public
	 * @return	void
	 * @update	2011/5/20
	*/
	public static function brand_catch_goods()
	{
		$date = date('Ymd');
		$filename = TMPPATH.'/logs/cli/'.$date.'.sql';

		$select = DB::select('brand_id', 'id')
			->from('aggregate_brand')
			->where('created_time', '<', time());
		$iterator = new Db_Iterator($select, 'default', 20);

		while ($iterator->valid())
		{
			foreach ($iterator->current() as $row)
			{
				CLI::output("\n正在获取品牌".$row['brand_id']."的商品\n");

				$goods = DB::select('goods_id')
					->from('goods_products')
					->where('brand_props', '=', $row['brand_id'])
					->fetch_all();

				$goods_new = array();
				$set = array('created_time' => time());

				if ( !  empty($goods))
				{
					foreach ($goods as $g)
					{
						$goods_new[] = $g['goods_id'];
					}
					$set['ids'] = implode(',', $goods_new);
					$vid = explode(':', $row['brand_id']);
					$values = DB::select('name')
						->from('item_prop_values')
						->where('vid', '=', end($vid))
						->fetch_one();
					$explode = explode('/', $values);

					foreach ($explode as $v)
					{
						$v = strtoupper($v);

						$insert = array(
							'type' => 2,
							'sid' => $row['brand_id'],
							'name' => $v,
							'url' => 'brand.360hqb.com',
						);

						// 查找是否已存在相同记录
						$ok = DB::select('del', 'id')
							->from('aggregate_prop')
							->where('sid', '=', $row['brand_id'])
							->where('name', '=', trim($v))
							->where('type', '=', '2')
							->fetch_row();

						if (empty($ok))
						{
							// 不存在则插入记录
							$sql = DB::insert('aggregate_prop', array_keys($insert))
								->values(array_values($insert));
							@file_put_contents($filename, $sql.";\r\n", FILE_APPEND);

						}elseif ($ok['del'] > 1) {
							// 否则更新为启用
							$sql = DB::update('aggregate_prop')
								->set(array('del' => 0))
								->where('id', '=', $ok['id']);
							@file_put_contents($filename, $sql.";\r\n", FILE_APPEND);
						}
					}
				}else {
					CLI::output("..");
					// 删除不符合规则的记录
					$sql = DB::delete('aggregate_prop')
						->where('sid', '=', $row['brand_id'])
						->where('type', '=', '2')
						->where('del', '<>', '1');
					@file_put_contents($filename, $sql.";\r\n", FILE_APPEND);
				}
				// 更新为已抓取记录
				$sql = DB::update('aggregate_brand')
					->set($set)
					->where('id', '=', $row['id']);
				@file_put_contents($filename, $sql.";\r\n", FILE_APPEND);

				CLI::output("\n品牌".$row['brand_id']."的商品抓取完毕\n");
			}

			$iterator->next();
		}
		unset($iterator);
	} // end func

}


